<?php
    /**
     * Roles management class
     * @author anton
     *
     */
	class RolesController extends Controller
	{
		public $page_title = LANG_ROLES;
		public $layout = 'main';
		public $module = 'administration';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids|index');
		public $name = 'roles';
		public $menuItem = 'roles';
		
		/**
		 * Roles list
		 */
		function Index()
		{
			$parameters = array('num_rows');
			$this->data[$this->name] = $this->controller_model->ListItems($this->request->get, 'sp_roles_list_paged', $parameters);
			$this->data['num_rows'] = $parameters['num_rows'];
		}
		
		/**
		 * Role edit
		 * @param $id
		 */
		function Edit($id)
		{
			$this->data[$this->name] = $this->controller_model->Get($id);			
			if (!empty($this->request->post))
			{
				if ($this->request->post['refreshsystemaccess'] == 1)
				{
				    //refreshes tables list for the given role
					if ($this->controller_model->Update(array("id" => $id), 'sp_system_access_insert_for_role'))
					{
						$this->Redirect('system_access/edit/'.$this->request->post['id']);
					}
					else
					{
						$this->Redirect($this->name.'/edit/'.$this->request->post['id']);			
					}
				}
				else
				{
				    //updates the role
					if ($this->controller_model->Update($this->request->post))
					{
						$this->Redirect($this->name);
					}
					else
					{
						$this->data[$this->name] = $this->request->post;				
					}
				}
			}
			else
			{

			}	
		}
		
		/**
		 * Role add
		 */
		function Add()
		{						
			if (!empty($this->request->post))
			{
				if ($this->controller_model->Update($this->request->post))
				{
					$this->Redirect($this->name);
				}
				else
				{
					$this->data[$this->name] = $this->request->post;				
				}
			}
			else
			{

			}	
		}

		/**
		 * Role view
		 * @param $id
		 */
		function View($id)
		{
			$this->data[$this->name] = $this->controller_model->Get($id);
		}
		
		/**
		 * Role delete
		 * @param $id
		 * @param $confirm
		 */
		function Delete($id, $confirm = 0)
		{ 
			if ($confirm)
			{
				$this->controller_model->Delete($id);
				$this->Redirect($this->name);
			}
			else
			{
				$this->layout = "empty";
				$this->data[$this->name]['id'] = $id;
			}
		}
	}
?>